package com.sixeco.apigetway.controller;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sixeco.apigetway.common.CommonIO;
import com.sixeco.apigetway.common.CommonVar;
import com.sixeco.apigetway.handle.BcHttpHandle;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.AttributeKey;

/**
 * 抽象的controller
 * 
 * @author tianzhenjiu
 *
 */
public abstract class Controller {

	Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 标志是否初始化了channnel
	 */
	volatile boolean isInit = false;

	final Bootstrap httpbootstrap = new Bootstrap();





	/**
	 * 服务于请求
	 * 
	 * @param requestChannel
	 * @param request
	 * @param response
	 */
	public abstract void service(Channel requestChannel, HttpRequest request);

	/**
	 * 把报文路由给后置 服务
	 * 
	 * @param requestChannel
	 * @param httpPushVo
	 */
	protected void routeData(Channel requestChannel, FullHttpRequest request) {
		
		
		try {
			CommonIO.getInstance().bindAndWriteBcActiveChannel(requestChannel,0, request);
		} catch (Exception e) {
			logger.warn("异常",e);
			CommonIO.getInstance().writeMsgAndClose(requestChannel, CommonVar.createServerErrorRequest());
		}
	}



}
